from flask import Flask
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from config import ConfigType
from flask_shop.apis.api_register import register_app_routers
from sqlalchemy.orm import DeclarativeBase, MappedAsDataclass
from flask import jsonify
from flask_cors import CORS

app = Flask(
    import_name=__name__
)
CORS(app)

class Base(DeclarativeBase, MappedAsDataclass):
    __allow_unmapped__ = True

    def dict(self) -> dict:
        response = jsonify(self)
        return response.json


db: SQLAlchemy = SQLAlchemy(model_class=Base)
migrate = Migrate(app=app, db=db)


def init_app(flask_app: Flask, config_type: ConfigType):
    flask_app.config.from_object(config_type.value)
    db.init_app(flask_app)

    register_app_routers(flask_app)


init_app(flask_app=app, config_type=ConfigType.develop)

'''
数据库相关配置：
https://github.com/miguelgrinberg/Flask-Migrate
flask db init
flask db migrate
flask db upgrade
'''

"""
flask db stamp head
flask db migrate
flask db upgrade
"""
